package nina.yiyou.attendance.s10merge.input


import nina.yiyou.salary.input.util.CellValueGetter
import nina.yiyou.salary.input.util.ExcelReadOpener
import nina.yiyou.salary.input.util.HeaderMapMaker
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook

import java.nio.file.Path
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
import java.util.stream.Collectors
import java.util.stream.IntStream

/**
 * @see AttendPunchExcelV2Reader
 */
@Deprecated
class AttendPunchExcelReader {

  interface Line {

    /**
     * @return 员工姓名
     */
    String name()

    String department()

    LocalDate date()

    String weekday()

    LocalTime punchTime()

    Set<String> extraArg()

    Map<String, Object> toMap()
  }

  AttendPunchExcelReader(Path excelPath) {
    _excelPath = excelPath
  }

  List<Line> read() {
    Workbook workbook = new ExcelReadOpener(_excelPath).openXlsx()
    List result = readLines(workbook.getSheetAt(0))
    workbook.close()
    return result
  }

  private List<Line> readLines(Sheet sheet) {
    Map<Integer, String> header = new HeaderMapMaker(sheet.getRow(0)).make()
    def getCellVal = { Row r, int c -> new CellValueGetter(r.getCell(c)).get() }

    return IntStream.rangeClosed(1, sheet.lastRowNum)
        .mapToObj { sheet.getRow(it) }
        .map { header.collectEntries { col, head -> [head, getCellVal(it, col)] } as Map }
        .map { makeLine(it) }
        .collect(Collectors.toList())
  }

  private Line makeLine(Map lineMap) {
    String nameStr = lineMap['姓名']
    assert nameStr, lineMap

    Object dateStr = lineMap['日期']
    Object punchTimeStr = lineMap['实际打卡时间']

    return new Line() {
      @Override
      String name() {
        return nameStr
      }

      @Override
      String department() {
        return lineMap['部门']
      }

      @Override
      LocalDate date() {
        if (dateStr instanceof LocalDateTime) {
          return dateStr.toLocalDate()
        }
        return LocalDate.parse(dateStr as String, DATE_FMT)
      }

      @Override
      String weekday() {
        return lineMap['星期']
      }

      @Override
      LocalTime punchTime() {
        if (punchTimeStr instanceof LocalDateTime) {
          return punchTimeStr.toLocalTime()
        }
        try {
          return LocalTime.parse(punchTimeStr as String, TIME_FMT)
        } catch (DateTimeParseException ignored) {
          return null
        }
      }

      @Override
      Set<String> extraArg() {
        String argStr = lineMap['额外参数']
        return argStr ? Arrays.stream(argStr.split('\n'))
            .map { it.trim() }
            .collect(Collectors.toSet()) : []
      }

      @Override
      Map<String, Object> toMap() {
        return lineMap
      }
    }
  }

  private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern('yyyy/MM/dd')
  private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern('H:mm')

  private final Path _excelPath
}
